package com.wn.ticket.web.backstage;

import com.wn.ticket.common.ResponseEntity;
import com.wn.ticket.entity.HotSearch;
import com.wn.ticket.service.HotSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/backstage/hotsearch")
public class HotSearchController {

    @Autowired
    private HotSearchService hotSearchService;

    @PreAuthorize("hasAnyAuthority('super')")
    @RequestMapping("/add")
    public ResponseEntity add(@RequestBody HotSearch hotSearch) {
        try {
            hotSearchService.add(hotSearch);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("400", "fail", e.getMessage());
        }
        return ResponseEntity.success;
    }

    @PreAuthorize("hasAnyAuthority('super')")
    @RequestMapping("/deleted/{id}")
    public ResponseEntity deleted(@PathVariable int id) {
        try {
            hotSearchService.del(id);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity("400", "fail", e.getMessage());
        }
        return ResponseEntity.success;
    }

    @RequestMapping("/getAll")
    public ResponseEntity<HotSearch> getAll(){
        List<HotSearch> hotSearchList = hotSearchService.getAll();
        return new ResponseEntity("200","ok",hotSearchList);
    }
}
